package org.example.design.chainofresponsibility.test1;

import org.example.util.Function;

/**
 * @author huangshen
 * @version 0
 * @description Support
 * @since 2022/2/10 10:58
 */
public abstract class Support {

    private final String name;

    private Support next;

    protected Support(String name) {
        this.name = name;
    }

    public Support setNext (Support next) {
        this.next = next;
        return next;
    }

    public final void support(Trouble trouble) {
        if (resolve(trouble)) {
            done(trouble);
        } else if (!Function.equalsNull(next)) {
            next.support(trouble);
        } else {
            fail(trouble);
        }
    }

    /**
     * 解决问题的方法
     * @param trouble
     * @return boolean
     */
    protected abstract boolean resolve(Trouble trouble);

    protected void done(Trouble trouble) {
        System.out.println(trouble + "is resolved by " + this + ".");
    }

    protected void fail(Trouble trouble) {
        System.out.println(trouble + "cannot be resolved.");
    }

    @Override
    public String toString() {
        return "["+ name +"]";
    }
}
